/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */

import addProjectForm from '../label_form/add_project_form.vue';

import urlAdapter from 'src/home/mod_common/url_adapter';
const PROJECT_URLS = {
    get: urlAdapter('project/list'),
    delete: urlAdapter('project'),
    post:urlAdapter('project')
};

export default {
    data () {
        return {
            projectRecord:{},
            modelTreeData:[]
        };
    },
    computed: {
        projectName () {
            return this.$route.query.name || '';
        }
    },

    methods: {

        // 获取项目列表
        async refreshProjectData (type = '') {
            this.$mask();

            //卡片搜索关键字
            let jsonData = await this.$ajax.get(PROJECT_URLS.get);
            this.$unmask();
            let {success, msg, data} = jsonData;
            if (!success) {
                this.$showErr(msg || _('加载数据失败'));
                return;
            }
            if (type === 'add') {

                //如果是新增而且为第一个的时候，则让第一个select为true;
                data.forEach((item, index)=>{
                    item.selected = index === 0;
                });
            } 

            let projectName = this.$route.query.name;

            if (projectName) {
                data.forEach((item)=>{
                    item.selected = item.name === projectName;
                });
            }
            let projectData = [{
                name:_('所有项目'),
                children:data,
                description: _('所有项目')
            }];
            this.modelTreeData = projectData;
        },

        // 新增项目弹窗
        _createProjectFormWin () {
            this.projectFormWin = this.$modal(addProjectForm, {
                autoDestory: true
            });
        },

        //新增项目
        _addProject () {
            this._createProjectFormWin();
            this.projectFormWin.open({
                reset: true,
                title: _('新增项目'),
                submit: () => {
                    let ajaxParams = this.projectFormWin.formRoot.getJsonValue();
                    this.ajaxSubmitProject(ajaxParams, this.projectFormWin);
                }
            });
        },

        //编辑项目
        _editProject (record) {
            this._createProjectFormWin();
            this.projectFormWin.formRoot.setJsonValue(record);
            this.projectFormWin.open({
                title: _('编辑项目'),
                data:{
                    isEdit:true
                },
                submit: () => {
                    let ajaxParams = this.projectFormWin.formRoot.getJsonValue();
                    this.ajaxSubmitProject(ajaxParams, this.projectFormWin, 'put');
                }
            });
        },

        // 新增/编辑算法的卡片信息
        async ajaxSubmitProject (params, formWin, fetch = 'post') {
            formWin.$mask();
            let ADD_PROJECT_URL = `${PROJECT_URLS.post}/${params.name}`;
            let jsonData = await this.$ajax[fetch](ADD_PROJECT_URL, params);
            formWin.$unmask();
            let {success, msg, data} = jsonData;
            if (!success) {
                return this.$showErr(msg || _('保存失败'));
            }
            this.$ok(msg || _('保存成功'));
            formWin.hide();
            this.refreshProjectData('add');
            let record = {
                id:data.id,
                name:data.name
            };

            //新增完，要跳转到特征探索。
            if (fetch === 'post') {
                this._jumpProjectIndex(record);
            }
        },

        // 删除项目
        _deleteProject (record) {
            let params = {
                id: record.id,
                name:record.name
            };
            this.$confirm({
                title: _('提示'),
                subTitle: _('确定要删除项目'),
                msg: _('删除项目后，该项目下的所有内容将会被删除'),
                icon: 'warning',
                autoClose: true,
                callback: actionName => {
                    if (actionName === 'submit') {
                        this.ajaxDeleteProject(params);
                    }
                }
            });
        },
      

        // 执行删除操作
        async ajaxDeleteProject (params) {
            this.$mask();
            let jsonData = await this.$ajax.delete(`${PROJECT_URLS.delete}/${params.id}`, params);
            this.$unmask();
            let {success, msg} = jsonData;
            if (!success) {
                return this.$showErr(msg || _('删除失败'));
            }
            this.$ok(_('删除成功'));

            //删除选中项目name;
            if (params.name === this.projectName) {
                this.$router.push({
                    path: '/project_manager'
                });
            }
            this.refreshProjectData();

        },

        _jumpProjectIndex (record) {
            this.$router.push({
                path: '/project_manager/project_index/project_view',
                query:{name:record.name, projectId:record.id}
            });
        }
    }
};
